package com.cloudsigns.factorymanager2.engineering.model;

import android.text.TextUtils;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.engineering.bean.RegionBean;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/5/5.
 */

public class RegionListModel implements InRegionListModel {

    @Override
    public void getMediaRegionList(final OnMediaRegionListListener mediaRegionListListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                HttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(ProjectUtil.REGION_LIST_URL);

                HttpResponse response;
                try {
                    httpGet.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpGet.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);

                    response = client.execute(httpGet);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    if (null != mediaRegionListListener) {
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            JSONArray jsonArr = new JSONArray(json.getString("regionList"));
                            List<RegionBean> regionBeanList = new ArrayList<RegionBean>();
                            if (jsonArr != null) {

                                for (int i = 0; i < jsonArr.length(); i++) {
                                    JSONObject jsonObj = jsonArr.getJSONObject(i);
                                    if (jsonObj != null) {
                                        RegionBean regionBean = new RegionBean();
                                        regionBean.setCompanyId(jsonObj.getString("companyId"));
                                        regionBean.setId(jsonObj.getString("id"));

                                        String latStr = jsonObj.getString("lat");
                                        if (!TextUtils.isEmpty(latStr)) {
                                            regionBean.setLat(Double.parseDouble(latStr));
                                        }

                                        String lonStr = jsonObj.getString("lon");
                                        if (!TextUtils.isEmpty(lonStr)) {
                                            regionBean.setLon(Double.parseDouble(lonStr));
                                        }

                                        regionBean.setRegionName(jsonObj.getString("regionName"));

                                        regionBeanList.add(regionBean);

                                    }
                                }

                                mediaRegionListListener.getMediaRegionListSuccessed(regionBeanList);

                            }


                        } else {
                            String error_str = code;
                            error_str += "," + ConstantUtil.getErrorCode(code);
                            if (!json.isNull("error_str")) {
                                error_str += "," + json.getString("error_str");
                            }


                            mediaRegionListListener.getMediaRegionListFailed(error_str);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }
            }
        }).start();
    }

    public interface OnMediaRegionListListener {
        void getMediaRegionListSuccessed(List<RegionBean> regionBeanList);

        void getMediaRegionListFailed(String desc);
    }
}
